﻿using System;
using System.Reflection;
using System.Text;
using MugenInjection.Infrastructure;
using MugenInjection.Interface;

namespace MugenInjection.Exceptions
{
    /// <summary>
    /// The exception that is thrown when you activate the object.
    /// </summary>
#if !SILVERLIGHT
#if !NETFX_CORE
    [Serializable]
#endif
#endif
    public class ActivatorException : Exception
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivatorException"/> class.
        /// </summary>
        /// <param name="message">The error message that explains the reason for the exception. </param>
        public ActivatorException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivatorException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
        /// </summary>
        /// <param name="message">The error message that explains the reason for the exception. </param><param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. </param>
        public ActivatorException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        #endregion

        #region Method

        internal static ActivatorException CreateActivatorException(IBindingContext bindingContext, ConstructorInfo constructorInfo, Exception innerException)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("When create a service {0} error has occurred, more detailed information see in the inner exception.", InjectorUtilsInternal.FormatType(constructorInfo.DeclaringType)));
            sb.AppendLine("The activation  path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return new ActivatorException(sb.ToString(), innerException);
        }

        internal static ActivatorException CreateActivatorException(object target, IBindingContext bindingContext, Exception innerException)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("When activate a service {0} error has occurred, more detailed information see in the inner exception.", InjectorUtilsInternal.FormatType(target.GetType())));
            sb.AppendLine("The activation path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return new ActivatorException(sb.ToString(), innerException);
        }

        internal static ActivatorException CreateActivatorNullConstructorException(IBindingContext bindingContext)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("No constructor was available to create an instance of the {0}.", InjectorUtilsInternal.FormatType(bindingContext.Service)));
            sb.AppendLine("If this value type, make sure there is have constructor.");
            sb.AppendLine("The activation path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return new ActivatorException(sb.ToString());
        }

        internal static ActivatorException CreateNamedAttributeActivatorException(string name, IBindingContext bindingContext, Exception innerException)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("When activate a service {0} using an attribute NamedParameterAttribute with name {1}, error has occurred, more detailed information see in the inner exception.",
                InjectorUtilsInternal.FormatType(bindingContext.Service), name));
            sb.AppendLine("The activation path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return new ActivatorException(sb.ToString(), innerException);
        }

        #endregion

    }
}